/*
 * 文件名: BaseTabParentActivity.java
 * 版    权：  Copyright PingAn Technology All Rights Reserved.
 * 描    述: [tabWiget父Activity的基类,封装了滑动动画等方法]
 * 创建人: EX-YUWEI001
 * 创建时间: 2011-12-22
 * 
 * 修改人：
 * 修改时间:
 * 修改内容：[修改内容]
 */
package com.longevityassistant.activity;

import java.util.ArrayList;
import java.util.HashMap;

import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ViewFlipper;
import app.log.APPLog;
import app.widget.MoveAnimationListener;

/**
 * [tabWiget父Activity的基类,封装了滑动动画等方法]<BR>
 * [功能详细描述]
 * 
 * @author EX-YUWEI001
 * @version [Android PABank C01, 2011-12-22]
 */
public abstract class BaseTabParentActivity extends BaseActivity implements
		OnCheckedChangeListener {

	/**
	 * tab背景动画滑动速度
	 */
	private HashMap<Integer, ArrayList<BaseTabActivity>> mActivityStacks;
	protected int lastIndex = 100;
	BaseTabActivity currentActivity;

	/**
	 * TODO tabbar滑动动画
	 * 
	 * @param group
	 *            切换 的radioGroup
	 * @param bgView
	 *            滑动Image
	 * @param checkId
	 *            当前选中的ID
	 */
	protected void startAnimation(RadioGroup group, ImageView bgView,
			int checkId) {
		// TODO Auto-generated method stub

		bgView.clearAnimation();
		RadioButton tagetBtn = (RadioButton) findViewById(checkId);
		APPLog.v("move", "bgViewX=" + bgView.getLeft() + "  tagetViewX="
				+ tagetBtn.getLeft() + "  radioGoupX=" + group.getLeft());
		for (int i = 0; i < group.getChildCount(); i++) {
			RadioButton button = (RadioButton) group.getChildAt(i);
			button.setEnabled(true);
		}
		int fromX = bgView.getLeft();
		int toX = tagetBtn.getLeft() + group.getLeft();
		int disX = toX - fromX;
		APPLog.v("move", "fromX=" + fromX + "  toX" + toX);
		APPLog.v("move", "groupLeft=" + group.getLeft() + "radioLeft"
				+ tagetBtn.getLeft());
		TranslateAnimation animate = new TranslateAnimation(0, disX, 0, 0);
		animate.setDuration(500);
		MoveAnimationListener listener = new MoveAnimationListener(group,
				tagetBtn, toX, bgView);
		animate.setAnimationListener(listener);
		bgView.startAnimation(animate);

	}

	/**
	 * [一句话功能简述]<BR>
	 * [功能详细描述]
	 * 
	 * @param viewId
	 * @param instance
	 */
	protected void initView(int viewId, BaseTabParentActivity instance) {
		BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + viewId);
		if (!activity.isInit) {
			activity.setParentActivity(instance);
			activity.init();

		}

	}

	/**
	 * [切换View]<BR>
	 * [功能详细描述]
	 * 
	 * @param index
	 * @param instance
	 * @param bodyView
	 */
	public void showView(int index, BaseTabParentActivity instance,
			ViewFlipper bodyView, int requestCode, Object... data) {
		// TODO 如果当前显示的页面已经是你要显示的页面,则不做任何处理
		initView(index, instance);
		BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + index);
		currentActivity = activity;
		activity.setBodyView(bodyView);
		if (index != lastIndex) {
			startLayoutAnimation(bodyView, index, requestCode, data);
			bodyView.setDisplayedChild(index);
		}
		lastIndex = index;
	}

	public void resumeView(int index, BaseTabParentActivity instance,
			ViewFlipper bodyView, int requestCode, Object... data) {
		int lastChild = bodyView.getDisplayedChild();
		if (lastIndex != index) {
			initView(index, instance);
		}
		BaseTabActivity leaveActivity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + lastChild);
		BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + index);
		currentActivity = activity;
		leaveActivity.onLeaveScreen();
		activity.onIntoScreen(requestCode, data);
		bodyView.setInAnimation(null);
		bodyView.setOutAnimation(null);
		bodyView.setDisplayedChild(index);
		lastIndex = index;
	}

	/**
	 * [切换View]<BR>
	 * [功能详细描述]
	 * 
	 * @param index
	 * @param instance
	 * @param bodyView
	 */
	public void showView(int index, BaseTabParentActivity instance,
			ViewFlipper bodyView) {
		initView(index, instance);
		startLayoutAnimation(bodyView, index);
		bodyView.setDisplayedChild(index);

	}

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		mActivityStacks = new HashMap<Integer, ArrayList<BaseTabActivity>>();
	}
	
	

	@Override
    protected void onDestroy()
    {
	    if(currentActivity!=null){
            currentActivity.hideKeybord();
        }
        super.onDestroy();
    }

    /**
	 * [开始页面滑动动画]<BR>
	 * [功能详细描述]
	 * 
	 * @param bodyView
	 * @param childId
	 */
	protected void startLayoutAnimation(ViewFlipper bodyView, int childId) {
		int currentChild = bodyView.getDisplayedChild();
		Animation rightInAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_right_in);
		Animation rightOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_right_out);
		Animation leftInAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_left_in);
		Animation leftOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_left_out);
		if (childId != currentChild) {
			if (currentChild > childId) {
				bodyView.setInAnimation(rightInAnimation);
				bodyView.setOutAnimation(rightOutAnimation);

			} else if (currentChild < childId) {
				bodyView.setInAnimation(leftInAnimation);
				bodyView.setOutAnimation(leftOutAnimation);

			}
		}

	}

	/**
	 * [开始页面滑动动画]<BR>
	 * [功能详细描述]
	 * 
	 * @param bodyView
	 * @param childId
	 */
	protected void startLayoutAnimation(ViewFlipper bodyView, int viewId,
			int request, Object... objects) {
		final int childId = viewId;
		int lastChild = bodyView.getDisplayedChild();
		final BaseTabActivity leaveActivity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + lastChild);
		final Object[] data = objects;
		final int requestCode = request;
		int currentChild = bodyView.getDisplayedChild();
		Animation rightInAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_right_in);
		Animation rightOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_right_out);
		Animation leftInAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_left_in);
		Animation leftOutAnimation = AnimationUtils.loadAnimation(this,
				R.anim.push_left_out);
		if (childId != currentChild) {
			if (currentChild > childId) {
				bodyView.setInAnimation(rightInAnimation);
				bodyView.setOutAnimation(rightOutAnimation);
				rightInAnimation.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
								.getActivity("activity" + childId);
						activity.onIntoScreen(requestCode, data);
						leaveActivity.onLeaveScreen();
					}
				});
			} else if (currentChild < childId) {
				bodyView.setInAnimation(leftInAnimation);
				bodyView.setOutAnimation(leftOutAnimation);
				leftInAnimation.setAnimationListener(new AnimationListener() {

					@Override
					public void onAnimationStart(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationRepeat(Animation animation) {
						// TODO Auto-generated method stub

					}

					@Override
					public void onAnimationEnd(Animation animation) {
						// TODO Auto-generated method stub
						BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
								.getActivity("activity" + childId);
						currentActivity = activity;
						activity.onIntoScreen(requestCode, data);
						leaveActivity.onLeaveScreen();
					}
				});

			}
		}

	}

	/** 子Activity中切换tab **/
	public void checkTab(int checkId) {

	};

	/** 子Activity中切换tab(非首次进入页面时) **/
	public void checkTab(RadioGroup radio, int checkId, ImageView bgView) {
		if (radio != null) {
			RadioButton radioBtn = (RadioButton) radio.findViewById(checkId);
			startAnimation(radio, bgView, checkId);
			radioBtn.setChecked(true);
		}
	}

	/**
	 * [切换某个tab下的Activity]<BR>
	 * [功能详细描述]
	 * 
	 * @param intent
	 *            要切换到的intent
	 */
	public void showActivity(Intent intent, BaseTabParentActivity instance,
			int checkId, ViewFlipper bodyView) {
		bodyView.removeViewAt(checkId);
		String activityId = "activity" + checkId;
		LocalActivityManager manager = getLocalActivityManager();
		Window window = manager.startActivity(activityId, intent);
		BaseTabActivity activity = (BaseTabActivity) getLocalActivityManager()
				.getActivity("activity" + checkId);
		currentActivity=activity;
		activity.setParentActivity(instance);
		activity.setSelfView(window.getDecorView());
		ArrayList<BaseTabActivity> baseActivityList = mActivityStacks
				.get(checkId);
		if (baseActivityList == null) {
			baseActivityList = new ArrayList<BaseTabActivity>();
			mActivityStacks.put(checkId, baseActivityList);
		}
		baseActivityList.add(activity);

		lastIndex = checkId;
		bodyView.addView(window.getDecorView(), checkId);
		if (checkId != lastIndex) {
			startLayoutAnimation(bodyView, checkId, 0);
		}
		activity.setBodyView(bodyView);
		bodyView.setDisplayedChild(checkId);
		initView(checkId, instance);

	}

	/** 首次进入页面时对tab进行切换 **/
	public void checkTab(RadioGroup radio, ImageView bgView, int checkId) {
		int maginLeft = 0;
		float tab2marginLeft = getResources().getDimension(
				R.dimen.tab2_margin_left);
		float tab3marginLeft = getResources().getDimension(
				R.dimen.tab3_margin_left);
		float tab4marginLeft = getResources().getDimension(
				R.dimen.tab4_margin_left);

		float btn3Width = getResources().getDimension(R.dimen.tabbar_btn_width);
		float tab4Width = getResources().getDimension(R.dimen.tab4_btn_width);
		float tab2Width = getResources().getDimension(R.dimen.tab2_btn_width);
		radio.measure(0, 0);
		float radioWidth = radio.getMeasuredWidth();

		bgView.clearAnimation();

		RelativeLayout.LayoutParams params = (LayoutParams) bgView
				.getLayoutParams();
		RadioButton tagetBtn = (RadioButton) radio.findViewById(checkId);
		tagetBtn.measure(0, 0);
		int radioLeft = (int) ((getWindowManager().getDefaultDisplay()
				.getWidth() - radioWidth) / 2);
		switch (checkId) {

		case R.id.tab_left:

			params.leftMargin = (int) tab2marginLeft;

			break;
		case R.id.tab_right:

			params.leftMargin = (int) (tab2marginLeft + dip2px(30) + tab2Width);

			break;

//		case R.id.tab_notice_left:
//
//			params.leftMargin = radioLeft;
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_notice_mid:
//
//			RadioButton noticeButton = (RadioButton) radio.getChildAt(0);
//			params.leftMargin = (int) (radioLeft + getBtnWidth(noticeButton));
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_notice_right:
//			for (int i = 0; i < 2; i++) {
//				RadioButton btn = (RadioButton) radio.getChildAt(1);
//				maginLeft += getBtnWidth(btn);
//			}
//			params.leftMargin = (int) (radioLeft + maginLeft);
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_wrap_btn1:
//
//			params.leftMargin = radioLeft;
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_wrap_btn2:
//
//			RadioButton wrapButton = (RadioButton) radio.getChildAt(0);
//			params.leftMargin = radioLeft + getBtnWidth(wrapButton);
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_wrap_btn3:
//			for (int i = 0; i < 2; i++) {
//				RadioButton btn = (RadioButton) radio.getChildAt(1);
//				maginLeft += getBtnWidth(btn);
//			}
//			params.leftMargin = radioLeft + maginLeft;
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_wrap_btn4:
//			for (int i = 0; i < 3; i++) {
//				RadioButton btn = (RadioButton) radio.getChildAt(1);
//				maginLeft += getBtnWidth(btn);
//			}
//			params.leftMargin = radioLeft + maginLeft;
//			params.width = tagetBtn.getMeasuredWidth();
//			break;
//		case R.id.tab_btn_left:
//
//			params.leftMargin = (int) tab3marginLeft;
//
//			break;
//		case R.id.tab_btn_mid:
//
//			params.leftMargin = (int) (tab3marginLeft + btn3Width);
//
//			break;
//		case R.id.tab_btn_right:
//
//			params.leftMargin = (int) (tab3marginLeft + 2 * btn3Width);
//
//			break;
//		case R.id.tab_btn1:
//
//			params.leftMargin = (int) (tab4marginLeft);
//
//			break;
//		case R.id.tab_btn2:
//
//			params.leftMargin = (int) (tab4marginLeft + tab4Width);
//
//			break;
//		case R.id.tab_btn3:
//
//			params.leftMargin = (int) (tab4marginLeft + tab4Width * 2);
//
//			break;
//		case R.id.tab_btn4:
//
//			params.leftMargin = (int) (tab4marginLeft + tab4Width * 3);
//
//			break;

		default:
			break;

		}
		bgView.setLayoutParams(params);
		radio.check(checkId);

	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
	}

	/**
	 * [dip转换为px]<BR>
	 * [功能详细描述]
	 * 
	 * @param dipValue
	 * @return
	 */
	public int dip2px(float dipValue) {
		final float scale = this.getResources().getDisplayMetrics().density;

		return (int) (dipValue * scale + 0.5f);

	}

	public int getBtnWidth(View view) {

		view.measure(0, 0);
		return view.getMeasuredWidth();

	}

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void hideSafeSoftKeyboard() {
		// TODO Auto-generated method stub
		super.hideSafeSoftKeyboard();
		if(currentActivity!=null){
		    currentActivity.hideKeybord();
		}
	}

}
